﻿
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using DataLayer.Model.Classes;
using DataLayer;
using DataLayer.Model.Objects;
using BuisnessLayer;

namespace Overview
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        
        delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
 
        [DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
        [DllImport("user32.dll")]
        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
 
        const uint WM_CHAR = 0x102;
        const uint WM_PASTE = 0x0302;
 
        static IntPtr m_hWndEdit = IntPtr.Zero;

        public MainWindow()
        {
            Singleton.Init();
            InitializeComponent();
            LoadProfiles();
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            SaveProfiles();
            this.Close();
        }

        private void SaveProfiles()
        {
            var profiles = new List<Profile>();
            foreach (ProfileControl control in stpnlProfiles.Children)
                profiles.Add(control.ProfileData);
            ProfileManager.SaveProfileData(profiles);
        }

        private void LoadProfiles(){
            foreach (var profile in ProfileManager.ReadProfiles())
                AddControlToStackList(FormProfileControl(profile));
        }
        private void BtmMinimize_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            var proc = new Process();
            proc.StartInfo.FileName = @"C:\Program Files\Notepad++\notepad++.exe";
            try
            {
                proc.Start();
            }catch(Exception ex){
                return;
            }

            proc.WaitForInputIdle();
            IntPtr hWnd = proc.MainWindowHandle;
            IntPtr hWndEdit = IntPtr.Zero;
            EnumChildWindows(hWnd, EnumChildWindowsCallback, hWndEdit);

            if (m_hWndEdit != null) // edit window found?
            {
                // Now you could use different messages to send text (WM_CHAR, WM_KEYDOWN, ...)
                // I decided to use the clipboard:
                // Copy some text to the clipboardthis is magic!
                Clipboard.SetText("this is magic!");
                // ... and just paste it to the target window
                SendMessage(m_hWndEdit, WM_PASTE, 0, 0);
            }

             
        }


        static bool EnumChildWindowsCallback(IntPtr hWnd, IntPtr lParam)
        {
            // Search for notepads edit window - if we find it "false" is returned (means stop enumerating windows)

            StringBuilder sb = new StringBuilder();
            GetClassName(hWnd, sb, 256);
            if (!sb.ToString().Contains("Edit"))
            {
                return true;
            }

            m_hWndEdit = hWnd; // Store the handle to notepads edit window (this is the window we want to send the messages to)
            return false;
        }

        public Type AskCharacterOccupation()
        {
            Type charOccupation = null;
            var classSelector = new ClassSelector(typeof(Character));
            if (classSelector.ShowDialog() == true)
                charOccupation = classSelector.Type;
            return charOccupation;
        }

        private void btnCreateNEwProfile_Click(object sender, RoutedEventArgs e)
        {
            var profileName = UserInteract.AskStringData(new AskerMessage{
                    ConfirmContent = "Create",
                    Message = "Enter profile name.",
                    Title ="Profile creation",
                    DiscardContent = "Cancel"
                });
            if (Singleton.AlreadeExists(profileName))
            {
               // NotifyAboutUsedName();
                return;
            }
            var profileOccupation = AskCharacterOccupation();
            var level = UserInteract.AskIntegerData(new AskerMessage
            {
                ConfirmContent = "Create",
                Message = "What level you character would be?",
                Title = "Profile creation",
                DiscardContent = "Cancel"
            });

            var createdProfile = new Profile(profileOccupation, level);
            createdProfile.Name = profileName;
            var profileControl = FormProfileControl(createdProfile);
            AddControlToStackList(profileControl);
        }

        private void AddControlToStackList(ProfileControl profileControl)
        {
            stpnlProfiles.Children.Add(profileControl);
        }

        private ProfileControl FormProfileControl(Profile pProfile)
        {
            //TODO: add real profile build logic
            var profileContrl = new ProfileControl
            {
                ProfileData = pProfile
            };
            return profileContrl;
        }

    }
}
